package com.share.exam;

import lombok.Data;

import java.util.*;

/**
 * @desc: 字符频率排序
 * @author:caifan
 * @date:2020/5/24
 */
public class Main1 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        while (in.hasNextLine()) {
            String line = in.nextLine();
            System.out.println(sort(line));
        }
    }

    /**
     * 根据字符输出频率排序，大写在小写前，相同频率的按字典排序
     * @param line
     * @return
     */
    private static String sort(String line) {
        Map<String, Integer> sortMap = new TreeMap<>();
        char[] chars = line.toCharArray();
        List<StrArray> strArrays = new ArrayList<>();
        for (char ch : chars) {
            String temp = String.valueOf(ch);
            if (null != sortMap.get(temp)) {
                sortMap.put(temp, sortMap.get(temp) + 1);
            } else {
                sortMap.put(temp, 1);
            }
        }

        List<StrArray> list = new ArrayList<>();
        StringBuffer buffer = new StringBuffer();
        for (Map.Entry<String, Integer> map : sortMap.entrySet()) {
            StrArray strArray = new StrArray();
            strArray.setCh(map.getKey());
            strArray.setCount(map.getValue());
            list.add(strArray);

        }
        list.stream().sorted();
        for (StrArray str : list) {
            buffer.append(fill(str.getCh(), str.getCount()));
        }
        return buffer.toString();

    }

    /**
     * 填充字符
     * @param ch
     * @param count
     * @return
     */
    private static String fill(String ch, int count) {
        StringBuffer buffer = new StringBuffer();
        for (int i=0;i<count;i++) {
            buffer.append(ch);
        }
        return buffer.toString();
    }

    @Data
    private static class StrArray implements Comparator<StrArray> {

        private int count;
        private String ch;


        @Override
        public int compare(StrArray o1, StrArray o2) {
            if (o1.getCount() >= o2.getCount()) {
                return 1;
            } else {
                return o1.getCh().compareTo(o2.getCh());
            }
        }
    }
}
